package org.study.debezium.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年01月03日
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "debezium.mysql")
public class MysqlDebeziumConfiguration {
    private String name;
    private String connectorClass;
    private String databaseHostname;
    private String databasePort;
    private String databaseUser;
    private String databasePassword;
    private String databaseServerId;
    private String databaseServerName;
    private String databaseHistory;
    private String databaseHistoryFilename;
    private String topicPrefix;
    private String offsetStorage;
    private String offsetStorageFilename;
    private int offsetFlushIntervalMs;
    private String tableIncludeList;
    private boolean includeSchemaChanges;

    public Properties buidProperties() {
        Properties pros = new Properties();
        pros.put("name", this.name);
        pros.put("connector.class", this.connectorClass);
        pros.put("database.hostname", this.databaseHostname);
        pros.put("database.port", this.databasePort);
        pros.put("database.user", this.databaseUser);
        pros.put("database.password", this.databasePassword);
        pros.put("database.server.id", this.databaseServerId);
        pros.put("database.server.name", this.databaseServerName);
        pros.put("database.history", this.databaseHistory);
        pros.put("database.history.file.filename", this.databaseHistoryFilename);
        pros.put("topic.prefix", this.topicPrefix);
        pros.put("offset.storage", this.offsetStorage);
        pros.put("offset.storage.file.filename", this.offsetStorageFilename);
        pros.put("offset.flush.interval.ms", this.offsetFlushIntervalMs);
        // MySQL中的[database.tableName]，多个表以,分割
        pros.put("table.include.list", this.tableIncludeList);
        pros.put("include.schema.changes", this.includeSchemaChanges);
        return pros;
    }

    private static void checkFileExists(String absolutePath) {
        String dir = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
        log.info("dir：{}", dir);
        String fileName = absolutePath.substring(absolutePath.lastIndexOf("/") + 1);
        log.info("file name：{}", fileName);
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        File file = new File(absolutePath);
        if (!file.exists()) {
            try {
                boolean res = file.createNewFile();
                log.info("{} 文件不存在，新建文件：{}", absolutePath, res);
            } catch (IOException e) {
                log.info("{} 文件新建失败, msg: {}", absolutePath, e.getMessage());
                throw new RuntimeException(e);
            }
        }
    }
}
